﻿Public Class frmRegiao

#Region "Declaração de variáveis"
    Private objRegiao As New Controle.ctrRegiao
    Private objDiaAtendimento As New Controle.ctrDiaAtendimento
    Private iIdRegiao As Int32
    Private iIdDiaAtendimento As Int32
    Private sMensagemValidacao As String = ""
#End Region

#Region "Propriedades"

    Public Property IDRegiao() As Int32
        Get
            Return Me.iIdRegiao
        End Get
        Set(ByVal value As Int32)
            Me.iIdRegiao = value
        End Set
    End Property

#End Region

#Region "Métodos"

    Private Sub frmEstado_alteraModo(ByVal eModoAtual As frmPadrao.eModo) Handles Me.alteraModo
        If (eModoAtual = eModo.eAguardar Or eModoAtual = eModo.eExcluir) Then
            Me.lblRegiao.Enabled = False
            Me.txtRegiao.Enabled = False
            Me.lblCodigo.Enabled = True
            Me.txtIdRegiao.Enabled = True
            Me.cmdProcura.Enabled = True
            Me.fraDiaAtendimento.Enabled = False
        Else
            Me.lblRegiao.Enabled = True
            Me.txtRegiao.Enabled = True
            Me.lblCodigo.Enabled = False
            Me.txtIdRegiao.Enabled = False
            Me.cmdProcura.Enabled = False
            Me.fraDiaAtendimento.Enabled = True
        End If
    End Sub

    Private Sub frmEstado_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.txtIdRegiao.Focus()
        modoAtual = eModo.eAguardar

    End Sub

    Private Sub cmdGravar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGravar.Click
        Dim bRetorno As Boolean
        Try

            If Me.validarFormulario Then
                bRetorno = Me.objRegiao.salvar(Me.Codigo, _
                                               Me.iIdDiaAtendimento, _
                                               Me.txtRegiao.Text, _
                                               Me.chkSegunda.Checked, _
                                               Me.chkTerca.Checked, _
                                               Me.chkQuarta.Checked, _
                                               Me.chkQuinta.Checked, _
                                               Me.chkSexta.Checked, _
                                               Me.chkSabado.Checked)

                If bRetorno Then
                    MyBase.Codigo = Me.objRegiao.IDRegiao
                    MyBase.modoAtual = eModo.eAguardar
                    Me.txtIdRegiao.Text = MyBase.Codigo
                    Me.IDRegiao = MyBase.Codigo
                End If
            Else
                MsgBox(Me.sMensagemValidacao, MsgBoxStyle.Exclamation, Me.Text)
                Me.cmdCancelar.Text = "Cancelar"
                MyBase.modoAtual = eModo.eEditar
                Exit Sub
            End If

        Catch ex As Exception
            Me.Limpar(Me)
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub cmdExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExcluir.Click
        Try
            If MyBase.Exclui Then
                If Me.objRegiao.excluir(Convert.ToInt32(Me.txtIdRegiao.Text), Me.iIdDiaAtendimento) Then
                    MyBase.Codigo = 0
                    Me.IDRegiao = 0
                    MyBase.Limpar(Me)
                    MyBase.modoAtual = eModo.eAguardar
                    Me.txtIdRegiao.Focus()
                End If
            End If
        Catch ex As Exception
            MyBase.Codigo = 0
            Me.IDRegiao = 0
            MyBase.modoAtual = eModo.eAguardar
            Me.Limpar(Me)
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub cmdNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNovo.Click
        Me.txtRegiao.Focus()
        Me.IDRegiao = 0
    End Sub

    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        If Me.cmdCancelar.Text = "Alterar" Then
            Me.txtIdRegiao.Focus()
        Else
            Me.txtRegiao.Focus()
        End If
    End Sub

    Private Function validarFormulario() As Boolean

        Dim bRetorno As Boolean = True

        If Me.txtRegiao.Text = String.Empty Then
            sMensagemValidacao = "- Região obrigatória."
            bRetorno = False
        End If

        Return bRetorno

    End Function

    Private Sub carregaDados()
        Dim drDados As DataRow
        Dim drDiaAtendimento As DataRow
        Try
            If Me.IDRegiao > 0 Then
                drDados = Me.objRegiao.selecionarRegiao(Me.IDRegiao)

                Me.txtRegiao.Text = drDados.Item("Regiao")

                Me.iIdDiaAtendimento = Persistencia.perConversao.ToInt32(drDados.Item("IDDiaAtendimento"))

                If Me.iIdDiaAtendimento > 0 Then
                    drDiaAtendimento = Me.objDiaAtendimento.selecionarDiaAtendimento(Me.iIdDiaAtendimento)
                    If Not drDiaAtendimento Is Nothing Then
                        With drDiaAtendimento
                            Me.chkSegunda.Checked = Persistencia.perConversao.ToBoolean(.Item("Segunda"))
                            Me.chkTerca.Checked = Persistencia.perConversao.ToBoolean(.Item("Terca"))
                            Me.chkQuarta.Checked = Persistencia.perConversao.ToBoolean(.Item("Quarta"))
                            Me.chkQuinta.Checked = Persistencia.perConversao.ToBoolean(.Item("Quinta"))
                            Me.chkSexta.Checked = Persistencia.perConversao.ToBoolean(.Item("Sexta"))
                            Me.chkSabado.Checked = Persistencia.perConversao.ToBoolean(.Item("Sabado"))
                        End With
                    End If
                End If

            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub txtIdRegiao_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIdRegiao.LostFocus
        Dim drDados As DataRow

        Try
            If Me.txtIdRegiao.Text <> String.Empty Then
                drDados = Me.objRegiao.selecionarRegiao(Convert.ToInt32(Me.txtIdRegiao.Text))
                If Not drDados Is Nothing Then
                    Me.txtRegiao.Text = drDados.Item("Regiao")
                    MyBase.Codigo = drDados.Item("IDRegiao")
                    Me.IDRegiao = MyBase.Codigo
                    MyBase.modoAtual = eModo.eAguardar
                Else
                    MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub cmdProcura_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcura.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objRegiao.sqlConsulta

        With frmDialogo
            .Campos = "iIDRegiao,sRegiao"
            .Descricoes = "Código,Descrição"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de Regiões"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    MyBase.Codigo = .Id
                    Me.txtIdRegiao.Text = Me.Codigo
                    Me.IDRegiao = MyBase.Codigo
                    Me.carregaDados()
                    Me.modoAtual = eModo.eAguardar
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If

        End With
    End Sub

#End Region

End Class
